library(survey)
library(dplyr)
library(stargazer)
library(performance)

## creating Model for Table 3.1 
# model: multivariate model based on demographics, logit specification, DV: Q8_d 
# Q18a for "looks like me"

summary(factor(mil_conf.df$Q18))
sum(summary(factor(mil_conf.df$Q18)))-23
mil_conf.df2 <- mil_conf.df[mil_conf.df$Q18 < 98,]
length(mil_conf.df2$Q18)
summary(factor(mil_conf.df2$Q18))

# Q18 - "Looks like me" - 1 for most and some look like me
mil_conf.df2$Q18a <- 0
mil_conf.df2$Q18a[mil_conf.df2$Q18 < 3] <- 1
colnames(mil_conf.df2)

# Create race binary variables
mil_conf.df2$white <- 0
mil_conf.df2$white[mil_conf.df2$RACETHNICITY == 1] <- 1

mil_conf.df2$asian <- 0
mil_conf.df2$asian[mil_conf.df2$RACETHNICITY == 6] <- 1

# Create educ5
summary(factor(mil_conf.df2$EDUC))
mil_conf.df2$educ5[mil_conf.df2$EDUC < 9] <- 1
mil_conf.df2$educ5[mil_conf.df2$EDUC == 9] <- 2
mil_conf.df2$educ5[mil_conf.df2$EDUC ==10 | mil_conf.df2$EDUC == 11] <- 3
mil_conf.df2$educ5[mil_conf.df2$EDUC == 12] <- 4
mil_conf.df2$educ5[mil_conf.df2$EDUC > 12] <- 5

# Create silent
mil_conf.df2$silent <- 0
mil_conf.df2$silent[mil_conf.df2$generation==1] <- 1

## Create weighted survey design object
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df2
  )

summary(factor(w1_design$variables$Q8_d))
summary(factor(w1_design$variables$Q18a))

fig3.1_mod <- svyglm(Q8_d ~ Q18a + dem + rep + ideo3 + male + white + black + hispanic + asian + income5 +
                       educ5 + unemployed + silent + boomer + genx + millen + activeduty + vet +
                       social + family + midwest + south + west + catholic + christian + norelig +
                       city + rural + married + A_2 + A_3 + A_4 + A_5 + A_6 + A_7 + A_8,
                     family = binomial(link = "logit"),
                     design = w1_design)
summary(fig3.1_mod)

stargazer(fig3.1_mod)
check_collinearity(fig3.1_mod)
